package top.infopub.mgr.sg.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.sg.dao.CitySurveyAttDao;
import top.infopub.mgr.sg.dao.CitySurveyDao;
import top.infopub.mgr.sg.dao.CitySurveyLogDao;
import top.infopub.mgr.sg.service.CitySurveyService;
import top.infopub.mgr.sg.vo.CitySurveyVo;
import top.infopub.mgr.util.DateUtils;
import top.infopub.model.sg.CitySurvey;
import top.infopub.model.sg.CitySurveyAtt;
import top.infopub.model.sg.CitySurveyLog;
import top.infopub.security.util.SessionUserUtils;

import java.util.List;


@Service
public class CitySurveyServiceImpl extends GenericServiceImpl<CitySurvey, String> implements CitySurveyService {

    @Autowired
    private CitySurveyDao citySurveyDao;

    @Autowired
    private CitySurveyLogDao citySurveyLogDao;

    @Autowired
    private CitySurveyAttDao citySurveyAttDao;

    @Override
    public GenericDao<CitySurvey, String> getDao() {
        return citySurveyDao;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addCitySurvey(CitySurvey citySurvey, List<CitySurveyAtt> fileAtts) {
        citySurveyDao.insertById(citySurvey);

        CitySurveyLog citySurveyLog = new CitySurveyLog();
        citySurveyLog.setSurveyId(citySurvey.getId()); //主键作为citySurveyLog的城市调研信息id
        citySurveyLog.setSaveBy(SessionUserUtils.getSessionUserId());
        citySurveyLog.setSaveName(SessionUserUtils.getSessionAttributeForUserDtl().getRealName());
        citySurveyLog.setSaveTime(DateUtils.getCurrentDateTime());
        citySurveyLogDao.insert(citySurveyLog);

        //批量插入附件
        if (CollectionUtils.isNotEmpty(fileAtts)) {
            citySurveyAttDao.insertFileAtts(fileAtts);
        }
    }

    @Override
    public boolean selectByCityName(String cityName) {
        CitySurvey citySurvey = citySurveyDao.selectByCityName(cityName);
        if (null != citySurvey) {
            return true;
        }
        return false;
    }

    @Override
    public boolean selectByCityNameAndId(String cityName, String id) {
        CitySurvey t = new CitySurvey();
        if(StringUtils.isNotEmpty(cityName)){
            t.setCityName(cityName);
        }

        List<CitySurvey> selectByEntityWhere = citySurveyDao.selectByEntityWhere(t);
        if (selectByEntityWhere.size() ==0) {
            return false;
        }
        if(selectByEntityWhere.size() ==1){
            if(StringUtils.isNotEmpty(id)){
                CitySurvey dbCitySurvey = selectByEntityWhere.get(0);
                if(dbCitySurvey.getId().equals(id)){
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updtCitySurvey(CitySurvey citySurvey, List<CitySurveyAtt> fileAtts) {

        CitySurvey oldCitySurvey = citySurveyDao.selectByPrimaryKey(citySurvey.getId());
        citySurvey.setCityCode(oldCitySurvey.getCityCode());
        citySurvey.setState(oldCitySurvey.getState());
        citySurvey.setAddBy(oldCitySurvey.getAddBy());
        citySurvey.setAddTime(oldCitySurvey.getAddTime());
        BeanUtils.copyProperties(citySurvey, oldCitySurvey);
        this.updateByPrimaryKey(oldCitySurvey);

        //处理保存记录
        CitySurveyLog citySurveyLog = new CitySurveyLog();
        citySurveyLog.setSurveyId(citySurvey.getId()); //主键作为citySurveyLog的城市调研信息id
        citySurveyLog.setSaveBy(SessionUserUtils.getSessionUserId());
        citySurveyLog.setSaveName(SessionUserUtils.getSessionAttributeForUserDtl().getRealName());
        citySurveyLog.setSaveTime(DateUtils.getCurrentDateTime());
        citySurveyLogDao.insert(citySurveyLog);

        //先删除原附件
        citySurveyAttDao.deleteBySurveyId(citySurvey.getId());

        //批量插入附件
        if (CollectionUtils.isNotEmpty(fileAtts)) {
            citySurveyAttDao.insertFileAtts(fileAtts);
        }
    }

    @Override
    public Page<CitySurvey> selectPageByDomainWhere(Page<CitySurvey> page, CitySurvey citySurvey) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        Page<CitySurvey> citySurveys = citySurveyDao.selectByDomainWhere(citySurvey);
        return citySurveys;
    }

    @Override
    public CitySurvey getByCityName(String cityName) {
        CitySurvey citySurvey = citySurveyDao.selectByCityName(cityName);
        return citySurvey;
    }


    @Override
    public CitySurvey selectByCityId(String sgCityId) {
        return citySurveyDao.selectByPrimaryKey(sgCityId);
    }

    @Override
    public String selectByAddressId(CitySurveyVo citySurveyVo) {
        return citySurveyDao.selectByAddressId(citySurveyVo);
    }

    @Override
    public RspVo selectByAddress(String address) {
        String[] split = address.split(",");
        //根据省市区查询城市
        CitySurveyVo citySurveyVo = new CitySurveyVo();
        if (split.length>0){
            citySurveyVo.setRefProvinceId(split[0]); //省
        }
        if (split.length>1){
            citySurveyVo.setRefCityId(split[1]); //市
        }
        if (split.length>2){
            citySurveyVo.setRefAreaId(split[2]);//区
        }
        CitySurvey citySurvey = citySurveyDao.selectByAddress(citySurveyVo);
        if (citySurvey==null){
            return RspVo.getSuccessResponse(400,"城市调研信息不存在该城市");
        }
        return RspVo.getSuccessResponse();
    }
}
